<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="js/vue.js"></script>
    <script src="js/axios-0.18.0.js"></script>
</head>
<body>

<div id="app">
    <p>{{msg}}</p>

    <button @click="send()">发送异步请求-user</button>
    <button @click="send2()">发送异步请求-order</button>
</div>

</body>
<script>
    new Vue({
        el:"#app",
        data:{
            msg:""
        },
        methods:{
            send(){
                //先获取浏览器保存的token数据
                let token = localStorage.getItem("security_token");
                if (!token){ //js内部隐式转换，String不为null并且长度不为0，就为true
                    token = ""; //防止携带到后台的是一个null
                }

                //发送异步请求，访问8080服务器
                axios({
                    url:"http://localhost:8080/user/test1",
                    method:"get",
                    headers:{
                        token: token
                    }
                }).then(resp=>{
                    let r = resp.data;
                    let code = r.code;
                    if (code == 200){
                        this.msg = r.data;
                    }else if (code == 403){
                        //未登录
                        location.href = "login.html"
                    }else if (code == 401){
                        //没权限
                        location.href = "auth.html"
                    }
                })
            },

            send2(){
                //先获取浏览器保存的token数据
                let token = localStorage.getItem("security_token");
                if (!token){ //js内部隐式转换，String不为null并且长度不为0，就为true
                    token = ""; //防止携带到后台的是一个null
                }

                //发送异步请求，访问8080服务器
                axios({
                    url:"http://localhost:8080/order/test1",
                    method:"get",
                    headers:{
                        token: token
                    }
                }).then(resp=>{
                    let r = resp.data;
                    let code = r.code;
                    if (code == 200){
                        this.msg = r.data;
                    }else if (code == 403){
                        //未登录
                        location.href = "login.html"
                    }else if (code == 401){
                        //没权限
                        location.href = "auth.html"
                    }
                })
            }
        }
    })
</script>
</html>